package com.sun.androidexerciseproject.function.zxing;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.journeyapps.barcodescanner.CaptureActivity;
import com.sun.androidexerciseproject.function.R;

import pub.devrel.easypermissions.EasyPermissions;

import static com.sun.androidexerciseproject.function.zxing.QRCodeUtil.createQRCodeBitmap;

public class ZxingTestActivity extends AppCompatActivity {
    private static final int CAMERA_REQUEST_CODE = 1;
    /**
     * 请输入要输入的内容
     */
    private EditText mEtInput;
    private ImageView mIvQrImg;
    private TextView mTvQrContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zxing_test);
        initView();
    }

    public void scanQrCode(View view) {
        if (EasyPermissions.hasPermissions(ZxingTestActivity.this, Manifest.permission.CAMERA)) {
            doScanQrCode();
        } else {
            EasyPermissions.requestPermissions(ZxingTestActivity.this, "请给摄像头权限"
                    , CAMERA_REQUEST_CODE, Manifest.permission.CAMERA);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (CAMERA_REQUEST_CODE == requestCode) {
            int size = permissions.length;
            for (int i = 0; i < size; i++) {
                String str = permissions[i];
                if (Manifest.permission.CAMERA.equals(str)) {
                    if (PackageManager.PERMISSION_GRANTED == grantResults[i]) {
                        doScanQrCode();
                        break;
                    }
                }
            }
        }
    }

    private void doScanQrCode() {
        Intent intent = new Intent(ZxingTestActivity.this, CaptureActivity.class);
        startActivityForResult(intent, 0);
    }

    public void genQrCode(View view) {
        String str = mEtInput.getText().toString();
        if (!TextUtils.isEmpty(str)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int qrCodeSize = (int) getResources().getDimension(R.dimen.qrcode_size);
                    Bitmap bitmap = createQRCodeBitmap(str, qrCodeSize, qrCodeSize);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mIvQrImg.setImageBitmap(bitmap);
                        }
                    });
                }
            }).start();
        }
    }

    private void initView() {
        mEtInput = (EditText) findViewById(R.id.et_input);
        mIvQrImg = (ImageView) findViewById(R.id.iv_qr_img);
        mTvQrContent = (TextView) findViewById(R.id.tv_qr_content);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            String result = data.getExtras().getString("result");
            mTvQrContent.setText(result);
        }
    }
}
